#################
#Replication File for Strategic Rebels: A spatial econometric approach to rebel fighting durations in civil wars
#Authors: Nils W. Metternich and Julian Wucherpfennig
#Contacts: n.metternich@ucl.ac.uk, wucherpfennig@hertie-school.org
#Accepted for publication at International Interactions
#Last Update: 6 February 2020
#################
#  SYSTEM REQUIREMENTS
#  This script has been verfied to run under R version: R 3.6.2, OSX 10.15.3

# A: Before running the script create two folders 1) "data" and 2) "code"
# B: Update paths (pathDATA and pathCODE in lines 36/37 below) that point to the folders 1) "data" and 2) "code"
# C: Move files "WMs.rda" and "nsa.base.final.rda" to "data" folder
# D: Move files "estimation_functions_new.R", "postestimationII.R", and "comp.statII.R" to "code" folder
# E: Run IIanalysis.R: all tables and plots from paper will appear in "data" folder

#################
# Clear objects
#################
rm(list=ls())

#################
#Load Packages
#################
library(texreg)
library(stargazer)
library(dplyr)
library(Formula)
library(survival)
require(maxLik)
library(scales)

#################
#Set up paths
#################

pathDATA <- "YOURPATH/data"
pathCODE <- "YOURPATH/code"

###############
# Load data and connectivity matrix
###############
setwd(pathDATA)
load("WMs.rda")
load("nsa.base.final.rda")

#nsa.base.final and WMs are based on: 
# Non-state Actor data set v3.4: Cunningham,  David,  Kristian Skrede Gleditsch and Idean Salehyan. 2009.  “It takes two: A  dyadic  analysis  of  civil  war  duration  and  outcome.”Journal  of  Conflict  Resolution53(4):570–597.
# Control variables as outlined in manuscript from:
# UCDP actor data v2.1-2012: Petterson, Therese and Lotta Themner. 2012.UCDP Actor Dataset Codebook. Version 2.1-2012.  Department of Peace and Conflict Research, Uppsala University:  Uppsala ConflictData Program (UCDP).
# UCDP dyadic data v1-2013: Harbom, Lotta, Erik Melander and Peter Wallensteen. 2008. “Dyadic Dimensions of ArmedConflict, 1946-2007.”Journal of Peace Research45(5):697–710.
	



# Create country dummies that do not create perfect multicollinearity 
nsa.base <- nsa.base %>%
				dplyr::group_by(GWNOLoc) %>%
					mutate(countryfq = n())

nsa.base$GWNOLoc <- as.character(nsa.base$GWNOLoc)
	nsa.base$GWNOLoc[nsa.base$countryfq==1] <- "1001"
		nsa.base$GWNOLoc.S <- nsa.base$GWNOLoc
			nsa.base$GWNOLoc.S[nsa.base$GWNOLoc=="345"|nsa.base$GWNOLoc=="540"|nsa.base$GWNOLoc=="630"|nsa.base$GWNOLoc=="750"|nsa.base$GWNOLoc=="775"|nsa.base$GWNOLoc=="850"] <- "1002"
			nsa.base$GWNOLoc.S.L <- nsa.base$GWNOLoc.S
			nsa.base$GWNOLoc.S.L[nsa.base$GWNOLoc=="1002"|nsa.base$GWNOLoc=="200"|nsa.base$GWNOLoc=="230"|nsa.base$GWNOLoc=="344"|nsa.base$GWNOLoc=="346"|nsa.base$GWNOLoc=="365"|nsa.base$GWNOLoc=="372"|nsa.base$GWNOLoc=="475"|nsa.base$GWNOLoc=="490"|nsa.base$GWNOLoc=="530"|nsa.base$GWNOLoc=="600"|nsa.base$GWNOLoc=="710"|nsa.base$GWNOLoc=="780"] <- "1002"


#################
# Load estimation functions
#################
setwd(pathCODE)
source("estimation_functions_new.R")

################
#Table 2
################

f2n <- fight ~ log(wait)+ Splinter + Alliance +previous.gov.reb.vic+previous.agree +lpw +rebstrength + veto + start.order + secession + factor(GWNOLoc)
f2sp.v <- wait + fight ~ log(wait)+ Splinter + Alliance +previous.gov.reb.vic+previous.agree +lpw +rebstrength + veto + start.order + secession + factor(GWNOLoc)   
f2sp <- wait + fight ~ log(wait)+ Splinter + Alliance + previous.gov.reb.vic +previous.agree +lpw +rebstrength+ start.order  +  secession + factor(GWNOLoc)
f2sp.v <- wait + fight ~ log(wait)+ Splinter + Alliance +previous.gov.reb.vic+previous.agree +lpw +rebstrength + veto + start.order + secession + factor(GWNOLoc)  
f2sp.v.s <- wait + fight ~ log(wait)+Splinter + Alliance+ previous.gov.reb.vic+previous.agree+lpw +rebstrength +veto+ start.order  + factor(GWNOLoc)
f2sp.v.s.n <- wait + fight ~ log(wait)+Splinter + Alliance+ previous.gov.reb.vic+previous.agree+lpw +rebstrength +veto+ start.order + factor(GWNOLoc)
f2sp.v.out <- wait + fight ~ log(wait)+ Splinter + Alliance +previous.gov.reb.vic+previous.agree +lpw +rebstrength + veto + start.order + secession+gov.vic +reb.vic + agree + factor(GWNOLoc)  


n.weib <- nduration(fmla=f2n, dat=nsa.base, dist=n.weibull)
sp.weib <- spduration(fmla=f2sp, uniqID="spellID", dat=nsa.base, W=WMs, dist=sp.weibull)
sp.weib.v <- spduration(fmla=f2sp.v, uniqID="spellID", dat=nsa.base, W=WMs, dist=sp.weibull)
sp.weib.s <- spduration(fmla=f2sp.v.s, uniqID="spellID", dat=nsa.base[nsa.base$secession==1, ], W=WMs[nsa.base$secession==1,nsa.base$secession==1], dist=sp.weibull)
sp.weib.ns <- spduration(fmla=f2sp.v.s.n, uniqID="spellID", dat=nsa.base[nsa.base$secession==0 ,], W=WMs[nsa.base$secession==0 ,nsa.base$secession==0], dist=sp.weibull)

tex.model <- summary(n.weib$mle)
n.model <- n.weib$N
aic.model <- AIC(n.weib$mle)
bic.model <- AIC(n.weib$mle,k=log(n.weib$N))
tex.out.n.weib <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.weib$mle)
n.model <- sp.weib$N
aic.model <- AIC(sp.weib$mle)
bic.model <- AIC(sp.weib$mle,k=log(sp.weib$N))
tex.out.sp.weib <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.weib.v$mle)
n.model <- sp.weib.v$N
aic.model <- AIC(sp.weib.v$mle)
bic.model <- AIC(sp.weib.v$mle,k=log(sp.weib.v$N))
tex.out.sp.weib.v <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.weib.s$mle)  
n.model <- sp.weib.s$N
aic.model <- AIC(sp.weib.s$mle)
bic.model <- AIC(sp.weib.s$mle,k=log(sp.weib.s$N))
tex.out.sp.weib.s <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.weib.ns$mle)
n.model <- sp.weib.ns$N
aic.model <- AIC(sp.weib.ns$mle)
bic.model <- AIC(sp.weib.ns$mle,k=log(sp.weib.ns$N))
tex.out.sp.weib.ns <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))


coef.Names <- c("Intercept","Time until Entry$_{log}$","Splinter","Alliance","Previous Victory","Previous Agreement","Legal Political Wing","Rebel Strength","Veto","Order","Secession",rep("Country",length(unique(nsa.base$GWNOLoc))-1),"Scale$_{log}$","$\\rho$")
model.Names <- c("Veto Model","Spatial Model","Spatial Veto Model","Secession Model","Non-Secession Model")
setwd(pathDATA)
texreg(list(tex.out.n.weib,tex.out.sp.weib,tex.out.sp.weib.v,tex.out.sp.weib.s,tex.out.sp.weib.ns),use.packages=FALSE,dcolumn=TRUE,custom.coef.names=coef.Names,custom.model.names=model.Names,stars = c(0.01, 0.05, 0.1),scalebox=0.7,file="estSepAllCountry.tex",caption="Estimates for the main fighting duration models in accelerated failure time format. Standard errors are provided in parenthesis. Outcome variable is the fighting duration of rebel organizations measured in days.",label="tab:estSepAll",caption.above=TRUE)


################
#Table B3: K-dyadic Secession and Non-Secession model
################


f2n.s <- fight ~ log(wait)+ Splinter + Alliance +previous.gov.reb.vic+previous.agree +lpw +rebstrength + veto + start.order + factor(GWNOLoc.S)

n.weib.s <- nduration(fmla=f2n.s, dat=nsa.base[nsa.base$secession==1, ], dist=n.weibull)
n.weib.ns <- nduration(fmla=f2n.s, dat=nsa.base[nsa.base$secession==0,], dist=n.weibull)


tex.model <- summary(n.weib.s$mle)
n.model <- n.weib.s$N
aic.model <- AIC(n.weib.s$mle)
bic.model <- AIC(n.weib.s$mle,k=log(n.weib.s$N))
tex.out.n.weib.s <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(n.weib.ns$mle)
n.model <- n.weib.ns$N
aic.model <- AIC(n.weib.ns$mle)
bic.model <- AIC(n.weib.ns$mle,k=log(n.weib.ns$N))
tex.out.n.weib.ns <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))



coef.Names <- c("Intercept","Time until Entry$_{log}$","Splinter","Alliance","Previous Victory","Previous Agreement","Legal Political Wing","Rebel Strength","Veto","Order",rep("Country",length(unique(nsa.base$GWNOLoc.S))-1),"Scale$_{log}$")
model.Names <- c("K-dyadic Secession Model","K-dyadic Non-Secession Model")
setwd(pathDATA)
texreg(list(tex.out.n.weib.s,tex.out.n.weib.ns),use.packages=FALSE,dcolumn=TRUE,custom.coef.names=coef.Names,custom.model.names=model.Names,stars = c(0.01, 0.05, 0.1),scalebox=0.7,file="estSepBase.tex",caption="Estimates for k-dyadic secession and non-seccession models. Weibull duration models in accelerated failure time format. Standard errors are provided in parenthesis. Outcome variable is the fighting duration of rebel organizations measured in days.",label="tab:base",caption.above=TRUE)


################
# Figure 3, Figure 4, Figure C1
################
setwd(pathCODE)
source("postestimationII.R")

################
# Figure 5
################
setwd(pathCODE)
source("comp.statII.R")




################
#Table B4: Log-logistic and Lognormal models
################

f2sp.v <- wait + fight ~ log(wait)+ Splinter + Alliance +previous.gov.reb.vic+previous.agree +lpw +rebstrength + veto + start.order + secession   + factor(GWNOLoc)
f2sp.v.s <- wait + fight ~ log(wait)+Splinter + Alliance+ previous.gov.reb.vic+previous.agree+lpw +rebstrength +veto+ start.order  + factor(GWNOLoc.S)
#f2sp.v.s.n <- wait + fight ~ log(wait)+Splinter + Alliance+ previous.gov.reb.vic+previous.agree+lpw +rebstrength +veto+ start.order + factor(GWNOLoc)

### Log-logistic
sp.llog.v <- spduration(fmla=f2sp.v, uniqID="spellID", dat=nsa.base, W=WMs, dist=sp.loglogistic)
sp.llog.s <- spduration(fmla=f2sp.v.s, uniqID="spellID", dat=nsa.base[nsa.base$secession==1, ], W=WMs[nsa.base$secession==1,nsa.base$secession==1], dist=sp.loglogistic)
sp.llog.ns <- spduration(fmla=f2sp.v.s, uniqID="spellID", dat=nsa.base[nsa.base$secession==0 ,], W=WMs[nsa.base$secession==0 ,nsa.base$secession==0], dist=sp.loglogistic)

tex.model <- summary(sp.llog.v$mle)
n.model <- sp.llog.v$N
aic.model <- AIC(sp.llog.v$mle)
bic.model <- AIC(sp.llog.v$mle,k=log(sp.llog.v$N))
tex.out.sp.llog.v <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.llog.s$mle)
n.model <- sp.llog.s$N
aic.model <- AIC(sp.llog.s$mle)
bic.model <- AIC(sp.llog.s$mle,k=log(sp.llog.s$N))
tex.out.sp.llog.s <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.llog.ns$mle)
n.model <- sp.llog.ns$N
aic.model <- AIC(sp.llog.ns$mle)
bic.model <- AIC(sp.llog.ns$mle,k=log(sp.llog.ns$N))
tex.out.sp.llog.ns <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

### log-normal

f2sp.v.s <- wait + fight ~ log(wait)+Splinter + Alliance+ previous.gov.reb.vic+previous.agree+lpw +rebstrength +veto+ start.order  + factor(GWNOLoc.S.L)

sp.lnor.v <- spduration(fmla=f2sp.v, uniqID="spellID", dat=nsa.base, W=WMs, dist=sp.lognormal)
sp.lnor.s <- spduration(fmla=f2sp.v.s, uniqID="spellID", dat=nsa.base[nsa.base$secession==1, ], W=WMs[nsa.base$secession==1,nsa.base$secession==1], dist=sp.lognormal)
sp.lnor.ns <- spduration(fmla=f2sp.v.s, uniqID="spellID", dat=nsa.base[nsa.base$secession==0 ,], W=WMs[nsa.base$secession==0 ,nsa.base$secession==0], dist=sp.lognormal)

tex.model <- summary(sp.lnor.v$mle)
n.model <- sp.lnor.v$N
aic.model <- AIC(sp.lnor.v$mle)
bic.model <- AIC(sp.lnor.v$mle,k=log(sp.lnor.v$N))
tex.out.sp.lnor.v <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.lnor.s$mle)
n.model <- sp.lnor.s$N
aic.model <- AIC(sp.lnor.s$mle)
bic.model <- AIC(sp.lnor.s$mle,k=log(sp.lnor.s$N))
tex.out.sp.lnor.s <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.lnor.ns$mle)
n.model <- sp.lnor.ns$N
aic.model <- AIC(sp.lnor.ns$mle)
bic.model <- AIC(sp.lnor.ns$mle,k=log(sp.lnor.ns$N))
tex.out.sp.lnor.ns <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

coef.Names <- c("Intercept","Time until Entry$_{log}$","Splinter","Alliance","Previous Victory","Previous Agreement","Legal Political Wing","Rebel Strength","Veto","Order","Secession",rep("Country",length(unique(nsa.base$GWNOLoc))+97),"$\\rho$","Scale$_{log}$")
model.Names <- c("Spatial Veto","Secession","Non-Secession","Spatial Veto","Secession","Non-Secession")
texreg(list(tex.out.sp.llog.v,tex.out.sp.llog.s,tex.out.sp.llog.ns,tex.out.sp.lnor.v,tex.out.sp.lnor.s,tex.out.sp.lnor.ns),custom.coef.names=coef.Names,custom.model.names=model.Names,stars = c(0.01, 0.05, 0.1),scalebox=0.7,use.packages=FALSE,dcolumn=TRUE,file="estSepAllAlt.tex",caption="Estimates for log-logistic and log-normal spatial models using the main model specifications. Duration models in accelerated failure time format. Standard errors are provided in parenthesis. Outcome variable is the fighting duration of rebel organizations measured in days.",label="tab:estSepAllAlt",caption.above=TRUE)

################
#Table B5: Outcome models
################
f2sp.v.out <- wait + fight ~ log(wait)+ Splinter + Alliance +previous.gov.reb.vic+previous.agree +lpw +rebstrength + veto + start.order + secession+gov.vic +reb.vic + agree + factor(GWNOLoc)  

sp.weib.v.out <- spduration(fmla=f2sp.v.out, uniqID="spellID", dat=nsa.base, W=WMs, dist=sp.weibull)

tex.model <- summary(sp.weib.v.out$mle)
n.model <- sp.weib.v.out$N
aic.model <- AIC(sp.weib.v.out$mle)
bic.model <- AIC(sp.weib.v.out$mle,k=log(sp.weib.v.out$N))
tex.out.sp.weib.v.out <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

coef.Names <- c("Intercept","Time until Entry$_{log}$","Splinter","Alliance","Previous Victory","Previous Agreement","Legal Political Wing","Rebel Strength","Veto","Order","Secession","Government Victory","Rebel Victory","Agreement",rep("Country",length(unique(nsa.base$GWNOLoc))-1),"$\\rho$","Scale$_{log}$")
model.Names <- c("Spatial Veto Model controlling for outcomes")
setwd(pathDATA)
texreg(list(tex.out.sp.weib.v.out),use.packages=FALSE,dcolumn=TRUE,custom.coef.names=coef.Names,custom.model.names=model.Names,stars = c(0.01, 0.05, 0.1),scalebox=0.7,file="estSepAllCountryOut.tex",caption="Estimates for the main fighting duration model with outcome variables in accelerated failure time format. Standard errors are provided in parenthesis. Outcome variable is the fighting duration of rebel organizations measured in days.",label="tab:estSepAllOut",caption.above=TRUE)


################
#Table B6: Weak and strong rebels
################
# We now test differences for weak vs. strong rebel organizations. If we `disconnect' smaller or larger groups, do we see a difference?

WMsStrong <- WMs
WMsWeak <- WMs

WMsStrong[nsa.base$rebstrength<3,] <- 0
WMsWeak[nsa.base$rebstrength>=3,] <- 0
sp.weib.strong <- spduration(fmla=f2sp.v, uniqID="spellID", dat=nsa.base, W=WMsStrong, dist=sp.weibull)
sp.weib.weak <- spduration(fmla=f2sp.v, uniqID="spellID", dat=nsa.base, W=WMsWeak, dist=sp.weibull)


tex.model <- summary(sp.weib.strong$mle)
n.model <- sp.weib.strong$N
aic.model <- AIC(sp.weib.strong$mle)
bic.model <- AIC(sp.weib.strong$mle,k=log(sp.weib.strong$N))
tex.out.sp.weib.strong <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))


tex.model <- summary(sp.weib.weak$mle)
n.model <- sp.weib.weak$N
aic.model <- AIC(sp.weib.weak$mle)
bic.model <- AIC(sp.weib.weak$mle,k=log(sp.weib.weak$N))
tex.out.sp.weib.weak <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

coef.Names <- c("Intercept","Time until Entry$_{log}$","Splinter","Alliance","Previous Victory","Previous Agreement","Legal Political Wing","Rebel Strength","Veto","Order","Secession",rep("Country",length(unique(nsa.base$GWNOLoc))-1),"$\\rho$","Scale$_{log}$")
model.Names <- c("Strong rebel organizations","Weak rebel organizations")
setwd(pathDATA)
texreg(list(tex.out.sp.weib.strong,tex.out.sp.weib.weak),use.packages=FALSE,dcolumn=TRUE,custom.coef.names=coef.Names,custom.model.names=model.Names,stars = c(0.01, 0.05, 0.1),scalebox=0.7,file="estSepAllCountryStrong.tex",caption="Spatial duration models spatial impact only on strong and weak rebel organizations respectively. Estimates for fighting duration model in accelerated failure time format. Standard errors are provided in parenthesis. Outcome variable is the fighting duration of rebel organizations measured in days.",label="tab:estSepAllStrong",caption.above=TRUE)



################
#Table B7: Nested
################
n.weib <- nduration(fmla=f2n, dat=nsa.base[nsa.base$total.parties>1,], dist=n.weibull)
sp.weib <- spduration(fmla=f2sp, uniqID="spellID", dat=nsa.base[nsa.base$total.parties>1,], W=WMs[nsa.base$total.parties>1,nsa.base$total.parties>1], dist=sp.weibull)
sp.weib.v <- spduration(fmla=f2sp.v, uniqID="spellID", dat=nsa.base[nsa.base$total.parties>1,], W=WMs[nsa.base$total.parties>1,nsa.base$total.parties>1], dist=sp.weibull)

tex.model <- summary(n.weib$mle)
n.model <- n.weib$N
aic.model <- AIC(n.weib$mle)
bic.model <- AIC(n.weib$mle,k=log(n.weib$N))
tex.out.n.weib <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.weib$mle)
n.model <- sp.weib$N
aic.model <- AIC(sp.weib$mle)
bic.model <- AIC(sp.weib$mle,k=log(sp.weib$N))
tex.out.sp.weib <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

tex.model <- summary(sp.weib.v$mle)
n.model <- sp.weib.v$N
aic.model <- AIC(sp.weib.v$mle)
bic.model <- AIC(sp.weib.v$mle,k=log(sp.weib.v$N))
tex.out.sp.weib.v <- createTexreg(names(tex.model[[6]][,1]), tex.model[[6]][,1],tex.model[[6]][,2], pvalues = tex.model[[6]][,4], gof.names = c("Log-Likelihood","N","AIC","BIC"), gof.decimal=c(TRUE,FALSE,TRUE,TRUE), gof = c(tex.model[[5]],n.model,aic.model,bic.model))

coef.Names <- c("Intercept","Time until Entry$_{log}$","Splinter","Alliance","Previous Victory","Previous Agreement","Legal Political Wing","Rebel Strength","Veto","Order","Secession",rep("Country",length(unique(nsa.base$GWNOLoc))-15),"Scale$_{log}$","$\\rho$")
model.Names <- c("Veto Model","Spatial Model","Spatial Veto Model")
setwd(pathDATA)
texreg(list(tex.out.n.weib,tex.out.sp.weib,tex.out.sp.weib.v),use.packages=FALSE,dcolumn=TRUE,custom.coef.names=coef.Names,custom.model.names=model.Names,stars = c(0.01, 0.05, 0.1),scalebox=0.7,file="estSepAllParty.tex",caption="Estimates for main models including only rebel organizations nested in multi-actor conflicts. Weibull duration models in accelerated failure time format. Standard errors are provided in parenthesis. Outcome variable is the fighting duration of rebel organizations measured in days.",label="tab:estSepParty",caption.above=TRUE)


